
var newer = newer || {};
newer.gd = newer.gd || {};

newer.UriHelper = {
	PEOPLE_TYPE: 1,
	PROFILE_TYPE: 1,
	CIRCLE_TYPE: 2, 
	MYCIRCLE_TYPE: 3,
	HOME_TYPE: 0,
	
	initRedirection: function(o) {
		var toStatus = o.toStatus;
		var newUri = newer.UriHelper.statusToUri(toStatus);
		history.pushState({}, "", newUri);
		newer.UriHelper.getGlobals();
	},
	
	statusToUri: function(s) {
		// people page
		if (s.ptype == this.PEOPLE_TYPE) {
			if (s.tab != undefined) {
				return "/profile/"+s.id+"/"+s.tab+"/";
			} else {
				return "/profile/"+s.id+"/posts/";
			}
		} 
		// circle page
		else if (s.ptype == this.CIRCLE_TYPE) {
			if (s.tab != undefined) {
				return "/circles/"+s.id+"/"+s.tab+"/";
			} else {
				return "/circles/"+s.id+"/posts/";
			}	
		} 
		// home page
		else if(s.ptype == this.HOME_TYPE) {
			if (s.app != undefined ) {
				return "/home/"+s.app+"/";
			}
			return "/home/base/";
		}
		// cannot find
		return "/404";
	},
	
	uriToStatus: function(url) {
		var res = this.getUriSections(url);
		var status = {};
		if(res[0] == "circles" && res.length>=3) {
			status.ptype = this.CIRCLE_TYPE;
			status.id = res[1];
			status.tab = res[2];
		} else if(res[0] == "profile" && res.length>=3){
			status.ptype = this.PEOPLE_TYPE;
			status.id = res[1];
			status.tab = res[2];
		} else if(res[0] == "home" && res.length>=2){
			status.ptype = this.HOME_TYPE;
			status.appname = res[1];
		}
		return status;
	},
	
	// uriToStatus: function(url) {
		// var status = {};
		// if($.xurls({operation: "get", key: this.CIRCLE_TYPE, url: url}) != "") {
			// status.ptype = this.CIRCLE_TYPE;
			// status.id = $.xurls({operation: "get", key: this.CIRCLE_TYPE, url: url});
			// status.tab = $.xurls({operation: "get", key: "t", url: location.pathname});
		// } else if($.xurls({operation: "get", key: this.PEOPLE_TYPE, url: url}) != ""){
			// status.ptype = this.PEOPLE_TYPE;
			// status.id = $.xurls({operation: "get", key: this.PEOPLE_TYPE, url: url});
			// status.tab = $.xurls({operation: "get", key: "t", url: location.pathname});
		// } else {
			// status.ptype = this.HOME_TYPE;
			// status.appname = $.xurls({operation: "get", key: "a", url: url});
		// }
		// return status;
	// },
	
	updateBlobals: function() {
		this.getGlobals();
	},
	
	getGlobals: function(auth) {
		if (auth != undefined && auth == true) {
			this.getCurrentUserInfo();
		}

		this.getPageStatus();
		//this.getPageType(newer.gd);
		this.people = {};
		this.circle = {};
		switch(newer.gd.pstatus.ptype) {
			case this.PEOPLE_TYPE:
				this.getPeopleInfo();
				break;
			case this.CIRCLE_TYPE:
				this.getCircleInfo();
				break;
		}	
	},

	getPageStatus: function() {
		newer.gd.pstatus = this.uriToStatus(location.pathname);
	},
	
	getCurrentUserInfo: function() {
		var self = this;
		newer.gd.user = new Object();
		$.ajax({
			url: "/Login/getCurrentUserJSON",
			async: false,
			success: function(user) {
				if(user == false) {
					newer.gd.user.name = "";
					newer.gd.user.id = -1;
				} else {
					newer.gd.user = user;
				}
			}
		});
	},
	
	getPeopleInfo: function() {
		var self = this;
		newer.gd.people = new Object();
		newer.gd.people.id = newer.gd.pstatus.id;
		$.ajax({
			url: "/People/getPeopleInfo?pid="+newer.gd.people.id, 
			async: false, 
			success: function(people) {
				if(people.error) {
					// do something
				} else {
					newer.gd.people = people;
				}
			}
		});
	},
	
	getCircleInfo: function() {
		var self = this;
		newer.gd.circle = new Object();
		newer.gd.circle.id =  newer.gd.pstatus.id;
		$.ajax({
			url: "/Circles/getCircleInfo?cid="+newer.gd.circle.id, 
			async: false, 
			success: function(circle) {
				if(circle.error) {
					// do something
				} else {
					newer.gd.circle = circle;
				}
			}
		});
	},
	
	getUriSections: function(url) {
		console.log("url is " + url);
		var res = Array();
		var index = 1;
		var index2 = index;
		while(1) {
			console.log("index is " + index);
			console.log("index2 is " + index2);
			var index2 = url.substring(index).indexOf("/");
			if (index2 < 0) {
				res.push(url.substring(index));
				return res;
			}
			index2=index2+index;
			res.push(url.substring(index,index2));
			index=index2+1;
		};
	}
}